package com.fili.core.params;

import com.fili.core.keygen.EncodedPermission;
import com.fili.core.keygen.KeyEncoder;
import com.fili.gui.dialogs.RegisterMessageDialog;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 23, 2006
 * Time: 2:26:12 PM
 * Revision: 0
 */
public class DryConfig
{
    private static DryConfig _this;

    private EncodedPermission encodedPermission;
    private boolean demoMode;
    private boolean canRun;


    public static DryConfig getConfig()
    {
        if (_this == null)
            _this = new DryConfig();

        return _this;
    }

    public EncodedPermission getEncodedPermission()
    {
        return encodedPermission;
    }

    public boolean isDemoMode()
    {
        return demoMode;
    }

    public boolean canRun()
    {
        return canRun;
    }

    private DryConfig()
    {
        encodedPermission = KeyEncoder.match(DryParams.getParams().getUserName(), DryParams.getParams().getUserKey());
        if (encodedPermission != null)
        {
            canRun = true;
            demoMode = false;
        }
        else
        {
            demoMode = true;
            long time = System.currentTimeMillis();
            String message;
            long dif = 30 * 24 - (time - DryParams.getParams().getFirstRun()) / (1000 * 3600);
            if (dif < 0)
            {
                message = "Ati depasit perioada de evaluare.\nVa rugam inregistrati";
                canRun = false;
            }
            else
            {
                if (dif > 2 * 24)
                    message = "Mai aveti " + (dif / 24) + " zile de evaluare";
                else
                    if (dif > 24)
                        message = "Mai aveti o zi de evaluare";
                    else
                        message = "Mai aveti mai putin de o zi de evaluare";

                canRun = true;
                demoMode = true;
            }

            RegisterMessageDialog rmd = new RegisterMessageDialog(null, message, canRun);
            canRun = rmd.showModal();

            encodedPermission = KeyEncoder.match(DryParams.getParams().getUserName(), DryParams.getParams().getUserKey());
            if (encodedPermission == null)
            {
                demoMode = true;
                encodedPermission = new EncodedPermission(1, true, false, 0);
            }
            else
                demoMode = false;
        }
    }
}